package com.xpgk.mvc.intoPersonnel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.BentryRegisterService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerAgreement;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.measurement.Temporary;
import com.xpgk.mvc.measurement.TemporaryServices;
import com.xpgk.plugin.FileRenamePlugin;

import net.sf.json.JSONObject;

@Service(name = IntoPersonnelServices.serviceName)
public class IntoPersonnelServices extends BaseService{
	
	public static final String serviceName = "intoPersonnelServices";
	private static final Log log = Log.getLog(IntoPersonnelServices.class);
	/**
	 * 保存员工登记信息
	 * */
	public RenderBean  saveIntoPer(IntoPersonnel intoPersonnel,String personnelIds,String orgids)
	{	
		
	
		
		RenderBean result = new RenderBean();
		String  msg="";
		String idCard = intoPersonnel.getStr(IntoPersonnel.column_personnel_idcard);
		if(idCard == ""||idCard == null){
			msg="身份证不能为空！";
		}else{
			
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", IntoPersonnel.column_userIds); 
		String sql = getSqlByBeetl(IntoPersonnel.sqlId_column, param);
		IntoPersonnel personnel = IntoPersonnel.dao.findFirst(sql, intoPersonnel.getStr(IntoPersonnel.column_userIds));
		if(personnel!=null)
		{
			personnel.set(IntoPersonnel.column_fillingDate, intoPersonnel.getDate(IntoPersonnel.column_fillingDate))
			 .set(IntoPersonnel.column_personnel_sex, intoPersonnel.getStr(IntoPersonnel.column_personnel_sex))
			 .set(IntoPersonnel.column_personnel_birthday, intoPersonnel.getDate(IntoPersonnel.column_personnel_birthday))
			 .set(IntoPersonnel.column_personnel_height, intoPersonnel.getStr(IntoPersonnel.column_personnel_height))
			 .set(IntoPersonnel.column_personnel_weight, intoPersonnel.getStr(IntoPersonnel.column_personnel_weight))
			 .set(IntoPersonnel.column_personnel_nation, intoPersonnel.getStr(IntoPersonnel.column_personnel_nation))
			 .set(IntoPersonnel.column_personnel_politics_status, intoPersonnel.getStr(IntoPersonnel.column_personnel_politics_status))
			 .set(IntoPersonnel.column_personnel_marital_status, intoPersonnel.getStr(IntoPersonnel.column_personnel_marital_status))
			 .set(IntoPersonnel.column_personnel_health_condition, intoPersonnel.getStr(IntoPersonnel.column_personnel_health_condition))
			 .set(IntoPersonnel.column_personnel_major, intoPersonnel.getStr(IntoPersonnel.column_personnel_major))
			 .set(IntoPersonnel.column_personnel_education_background, intoPersonnel.getStr(IntoPersonnel.column_personnel_education_background))
			 .set(IntoPersonnel.column_personnel_computer_skill, intoPersonnel.getStr(IntoPersonnel.column_personnel_computer_skill))
			 .set(IntoPersonnel.column_personnel_graduation_school, intoPersonnel.getStr(IntoPersonnel.column_personnel_graduation_school))
			 .set(IntoPersonnel.column_personnel_idcard, intoPersonnel.getStr(IntoPersonnel.column_personnel_idcard))
			 .set(IntoPersonnel.column_province, intoPersonnel.getStr(IntoPersonnel.column_province))
			 .set(IntoPersonnel.column_city, intoPersonnel.getStr(IntoPersonnel.column_city))
			 .set(IntoPersonnel.column_area, intoPersonnel.getStr(IntoPersonnel.column_area))
			 .set(IntoPersonnel.column_personnel_address, intoPersonnel.getStr(IntoPersonnel.column_personnel_address))
			 .set(IntoPersonnel.column_introduce, intoPersonnel.getStr(IntoPersonnel.column_introduce))
			 .set(IntoPersonnel.column_jsrName, intoPersonnel.getStr(IntoPersonnel.column_jsrName))
			 .set(IntoPersonnel.column_salary_level, intoPersonnel.getStr(IntoPersonnel.column_salary_level))
			 .set(IntoPersonnel.column_self_assessment, intoPersonnel.getStr(IntoPersonnel.column_self_assessment))
			 .set(IntoPersonnel.column_employer_opinion, intoPersonnel.getStr(IntoPersonnel.column_employer_opinion))
			 .set(IntoPersonnel.column_personnel_department_opinion, intoPersonnel.getStr(IntoPersonnel.column_personnel_department_opinion))
			 .set(IntoPersonnel.column_leadership, intoPersonnel.getStr(IntoPersonnel.column_leadership))
			 .set(IntoPersonnel.column_picture, intoPersonnel.getStr(IntoPersonnel.column_picture));
			 
			if(!personnel.update())
			{
				log.debug("更新员工信息失败");
				throw new RuntimeException("更新员工信息失败!");
			}
			
		
			msg=ConstantRender.render_success_code;
			result.getMap().put("msg", msg);
//			result.getMap().put("customerIds", cc.getStr(Customer.column_ids));
		}else
		{
			if(intoPersonnel.save()){
				
				System.out.println("添加成功");
				msg=ConstantRender.render_success_code;
			
			}else{
				System.out.println("shibai ");
				msg="添加失败！";
			}
			result.getMap().put("msg", msg);
			return result;
		}
		}
		result.getMap().put("msg", msg);
		return result;
	}
	
	/**
	 * 保存员工工作经历信息
	 * */
	public RenderBean  saveExp(IntoExperience intoExperience)
	{
		
//		String msg=ConstantRender.render_success_code;
//			if(ieList!=null && ieList.size()>0)
//			{	
//				for(IntoExperience ie:ieList)
//				{
//					
//					if(!ie.save())
//					{
//						
//						msg=ConstantRender.render_error;
//						throw new RuntimeException("保存员工工作经历信息失败!");
//					}else{
//						msg=ConstantRender.render_success_code;
//						
//					}
//				}
//		}else{
//			msg=ConstantRender.render_error;
//		}
//			
//		return  msg;
		RenderBean result = new RenderBean();
		String  msg="";

			if(intoExperience.save()){
				
				System.out.println("添加成功");
				msg=ConstantRender.render_success_code;
			
					}else{
				System.out.println("shibai ");
				msg="添加失败！";
			}
			result.getMap().put("msg", msg);
			return result;
	
		
	}
	
	/**
	 * 保存员工直系亲属
	 * */

	
	public RenderBean  saveRel(IntoRelationship intoRelationship)
	{
		RenderBean result = new RenderBean();
		String  msg="";

			if(intoRelationship.save()){
				
				System.out.println("添加成功");
				msg=ConstantRender.render_success_code;
			
					}else{
				System.out.println("shibai ");
				msg="添加失败！";
			}
			result.getMap().put("msg", msg);
			return result;
	
		
	}
	/**上传正面照和侧面照
	 * 
	 * @param files
	 * @param cusIds
	 * @return
	 */
	public  JSONObject imageUpload(List<UploadFile>  files,String ids)
	{	
		 JSONObject json = new JSONObject();
		 Map<String,String>  nameMap=new  HashMap<>();
		 if(files!=null && files.size()>0)
		 {
			 for(int i=0;i<files.size();i++)
				{
					String uploadPath = files.get(i).getUploadPath();//获取保存文件的文件夹
			        String fileName = files.get(i).getFileName();//获取保存文件的文件名
			        String filePath = uploadPath+"\\"+fileName;//保存文件的路径
			        String extension = fileName.substring(fileName.lastIndexOf("."));
			        String fName=fileName.substring(0,fileName.lastIndexOf("."));
			        String resultFileName = FileRenamePlugin.instance.changeFileName(uploadPath+"\\"+fName,extension);//为了避免相同文件上传冲突，使用工具类重新命名
			        nameMap.put(files.get(i).getParameterName(), resultFileName);
			        File source = new File(filePath); 
			        String savePath = Code.savePicture_path;
			        if (".png".equals(extension) || ".jpg".equals(extension)
			                || ".gif".equals(extension) || "jpeg".equals(extension)
			                || "bmp".equals(extension)) {
			            try {
			                FileInputStream fis = new FileInputStream(source);

			                File targetDir = new File(savePath);
			                if (!targetDir.exists()) {
			                    targetDir.mkdirs();
			                }
			                File target = new File(targetDir, fileName);
			                if (!target.exists()) {
			                    target.createNewFile();
			                }
			                FileOutputStream fos = new FileOutputStream(target);
			                byte[] bts = new byte[1024 * 20];
			                while (fis.read(bts, 0, 1024 * 20) != -1) {
			                    fos.write(bts, 0, 1024 * 20);
			                }
			                fos.close();
			                fis.close();
			                json.put("status", 0);
			                source.delete();

			            } catch (FileNotFoundException e) {
			                json.put("status", 1);
			                json.put("message", "上传出现错误，请稍后再上传");
			            } catch (IOException e) {
			                json.put("status", 1);
			                json.put("message", "文件写入服务器出现错误，请稍后再上传");
			            }
			        } else {
			            source.delete();
			            json.put("status", 1);
			            json.put("message", "只允许上传png,jpg,jpeg,gif,bmp类型的图片文件");
			        }
				}
		 }else
		 {
			  json.put("status", 2);
	          json.put("message", "员工信息保存成功，未上传图片");
		 }
		 Map<String, Object> param = new HashMap<String, Object>();
			param.put("column", IntoPersonnel.column_userIds);
			String sql = getSqlByBeetl(IntoPersonnel.sqlId_column, param);
			IntoPersonnel intoPersonnel = IntoPersonnel.dao.findFirst(sql,ids);
			System.out.println("efjiodhfiudhfoihdfoi----------"+nameMap.get("zmfile"));
		if(intoPersonnel!=null)
		{
			
				intoPersonnel.set(IntoPersonnel.column_picture, nameMap.get("zmfile"))
					.update();
		}
		return json;
	}
	
}
